@app.route('/', methods=['GET', 'POST'])
def verify_code():
    if request.method == 'POST':
        access_code = request.form.get('access_code')
        logger.info(f"Access attempt with code: {access_code}")
        
        if access_code == '418':
            logger.info("Successful access, redirecting via JavaScript")
            return render_template_string("""
            <!doctype html>
            <html>
              <head>
                <meta charset="utf-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <title>Redirecting...</title>
                <script>
                  // 使用 setTimeout 延迟重定向，减少服务器压力
                  setTimeout(function() {
                    window.location.href = "https://duba.changshalib.cn/";
                  }, 500);
                </script>
                <style>
                  body {
                    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
                    background: linear-gradient(135deg, #74ebd5, #9face6);
                    height: 100vh;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    text-align: center;
                  }
                  .container {
                    background-color: white;
                    padding: 30px 40px;
                    border-radius: 10px;
                    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
                    width: 100%;
                    max-width: 450px;
                  }
                  h1 {
                    font-size: 24px;
                    color: #333;
                    margin-bottom: 20px;
                  }
                </style>
              </head>
              <body>
                <div class="container">
                  <h1>验证成功</h1>
                  <p>正在跳转到《长沙日历》多语种有声电子书...</p>
                  <p>如果页面没有自动跳转，请<a href="https://duba.changshalib.cn/">点击这里</a>。</p>
                </div>
              </body>
            </html>
            """)
        else:
            logger.warning(f"Invalid access code: {access_code}")
            error_message = "启动密码错误，请重试。"
            return render_template_string(form_template, error_message=error_message)
    
    logger.info("Page accessed")
    return render_template_string(form_template)